package com.developtools.view;

import com.developtools.model.ApiUploadModel;

import javax.swing.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

/**
 * @author jun
 */
public class ApiUploadView {
    private ApiUploadModel dataModel;


    private JPanel uploadPanel;
    private JPanel serverPanel;
    private JPanel modulePanel;
    private JTextField serverHost;
    private JComboBox<String> moduleBox;
    private JTextField tokenText;

    public ApiUploadView(ApiUploadModel apiUploadModel){
        this.dataModel = apiUploadModel;
        init();
    }

    public void init(){
        serverHost.setText(dataModel.getServerHost());
        for (String moduleName : dataModel.getModuleMap().keySet()) {
            moduleBox.addItem(moduleName);
        }
        moduleBox.addActionListener(e -> {
            String selectedItem = (String) moduleBox.getSelectedItem();
            dataModel.setSelectedModule(selectedItem);
            String token = dataModel.getModuleMap().get(selectedItem);
            tokenText.setText(token);
        });
        tokenText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                String selectedItem = (String) moduleBox.getSelectedItem();
                dataModel.getModuleMap().put(selectedItem, tokenText.getText());
            }
        });
        serverHost.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                dataModel.setServerHost(serverHost.getText());
            }
        });
    }

    public JPanel getUploadPanel() {
        return uploadPanel;
    }
}
